<?php

namespace App\Admin\Controllers;

use App\RoomType;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Encore\Admin\Widgets\Table;
use function foo\func;

class RoomTypeController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('房间类型')
            ->description('管理房间类型')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('详情')
            ->description('房间类型详情')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('修改')
            ->description('修改房间类型信息')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('创建新类型')
            ->description('创建新房间类型')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new RoomType);

        $grid->id('房间类型id');
        $grid->name('房间类型');
        $grid->intro('简介')->limit(30);
        $grid->rooms('房间')->display(function($rooms){
            $count = count($rooms);
            return "<span class='label label-primary'>$count</span>";
        })->expand(function($model){
            $rooms = $model->rooms()->get()->map(function($room){
                return $room->only(['id', 'number', 'created_at', 'reserved']);
            });
            return new Table(['房间ID', '房间号', '添加时间', '状态'], $rooms->toArray());
        });

        // filter
        $grid->filter(function($filter){
            $filter->disableIdFilter();
            $filter->like('name', '类型名称');
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(RoomType::findOrFail($id));

        $show->id('类型ID');
        $show->created_at('创建时间');
        $show->name('类型');
        $show->intro('简介');
        //TODO: 显示房间列表
//        $show->rooms('房间数量')->display(function($rooms){
//            $count = count($rooms);
//            return "<div>$count</div>";
//        });

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new RoomType);

        $form->text('name', '名称');
        $form->textarea('intro', '简介');

        return $form;
    }
}
